﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using VQWEB.administrator.sources;
using System.Web.Script.Serialization;
using VQData;
using VQWEB.common;
using System.Reflection;

namespace VQWEB.administrator.handlers
{
    /// <summary>
    /// Summary description for NganhHandler
    /// </summary>
    public class hn_NganhHandler : IHttpHandler,IRouteHandler
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        public void ProcessRequest(HttpContext context)
        {
            #region Nhom nganh
            string add_nhomnganh = context.Request["add_nhomnganh"];
            if (!string.IsNullOrEmpty(add_nhomnganh))
            {
                BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
                var obj = bussiness.InsertOrUpdate<hn_nhomnganh>(add_nhomnganh);
                context.Response.Write("Thực thi thành công !");
            }
            string view_nhomnganh = context.Request["view_nhomnganh"];
            if (!string.IsNullOrEmpty(view_nhomnganh))
            {
                string Id = context.Request["Id"];
                RenderSingleNhomnganh(context, Convert.ToInt32(Id));
            }
            string list_nhomnganh = context.Request["list_nhomnganh"];
            if (!string.IsNullOrEmpty(list_nhomnganh))
            {
                 RenderNhomnganh(context);
            }

            string delete_nhomnganh = context.Request["delete_nhomnganh"];
            if (!string.IsNullOrEmpty(delete_nhomnganh))
            {
                 BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
                 bussiness.Delete<hn_nhomnganh>(delete_nhomnganh);
                RenderNhomnganh(context);
            }

            string lockunlock_nhomnganh = context.Request["lockunlock_nhomnganh"];
            if (!string.IsNullOrEmpty(lockunlock_nhomnganh))
            {
                BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
                bussiness.LockUnLock<hn_nhomnganh>(lockunlock_nhomnganh, "nhomnganh_active");
                RenderNhomnganh(context);
            } 
            #endregion

            #region nganh
            string add_nganh = context.Request["add_nganh"];
            if (!string.IsNullOrEmpty(add_nganh))
            {
                BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
                var obj = bussiness.InsertOrUpdate<hn_nganh>(add_nganh);
                context.Response.Write("Thực thi thành công !");
            }
            string view_nganh = context.Request["view_nganh"];
            if (!string.IsNullOrEmpty(view_nganh))
            {
                string Id = context.Request["Id"];
                if (!string.IsNullOrEmpty(Id))
                    RenderSingleNganh(context, Convert.ToInt32(Id));
                else
                    RenderSelectNhomnganh(context);
            }
            string list_nganh = context.Request["list_nganh"];
            if (!string.IsNullOrEmpty(list_nganh))
            {
                RenderNganh(context);
            }

            string delete_nganh = context.Request["delete_nganh"];
            if (!string.IsNullOrEmpty(delete_nganh))
            {
                 BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
                 bussiness.Delete<hn_nganh>(delete_nganh);
                RenderNganh(context);
            }

            string lockunlock_nganh = context.Request["lockunlock_nganh"];
            if (!string.IsNullOrEmpty(lockunlock_nganh))
            {
                BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
                bussiness.LockUnLock<hn_nganh>(lockunlock_nganh, "nganh_active");
                RenderNganh(context);
            }
            #endregion

            #region chuyen nganh
            string add_chuyennganh = context.Request["add_chuyennganh"];
            if (!string.IsNullOrEmpty(add_chuyennganh))
            {
                BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
                var obj = bussiness.InsertOrUpdate<hn_chuyennganh>(add_chuyennganh);
                context.Response.Write("Thực thi thành công !");
            }
            string view_chuyennganh = context.Request["view_chuyennganh"];
            if (!string.IsNullOrEmpty(view_chuyennganh))
            {
                string Id = context.Request["Id"];
                if (!string.IsNullOrEmpty(Id))
                    RenderSingleChuyennganh(context, Convert.ToInt32(Id));
                else
                    RenderSelectNganh(context);
            }
            string list_chuyennganh = context.Request["list_chuyennganh"];
            if (!string.IsNullOrEmpty(list_chuyennganh))
            {
                RenderChuyennganh(context);
            }
            string delete_chuyennganh = context.Request["delete_chuyennganh"];
            if (!string.IsNullOrEmpty(delete_chuyennganh))
            {
                 BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
                 bussiness.Delete<hn_chuyennganh>(delete_chuyennganh);
                RenderChuyennganh(context);
            }

            string lockunlock_chuyennganh = context.Request["lockunlock_chuyennganh"];
            if (!string.IsNullOrEmpty(lockunlock_chuyennganh))
            {
                BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
                bussiness.LockUnLock<hn_hedaotao>(lockunlock_chuyennganh, "chuyennganh_active");
                RenderChuyennganh(context);
            }
            #endregion

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        #region Render Nhom nganh
        private void RenderSingleNhomnganh(HttpContext context, int Id)
        {
            DataLayer<HuongNghiepModelDataContext> layer = new DataLayer<HuongNghiepModelDataContext>();
            object obj = layer.Get<hn_nhomnganh>(
               nn => nn.nhomnganh_id.Equals(Id),
               nn => new
               {
                   id = nn.nhomnganh_id,
                   ma = nn.nhomnganh_ma,
                   ten = nn.nhomnganh_ten,
                   gioithieu = nn.nhomnganh_gioithieu,
                   active = nn.nhomnganh_active
               });
            string json = serializer.Serialize(obj);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
        private void RenderNhomnganh(HttpContext context)
        {
            BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
            IList<object> list = bussiness.List<hn_nhomnganh>(
                null,
                null,
                null,
                nn => nn.nhomnganh_ten,
                 nn => new
               {
                   id = nn.nhomnganh_id,
                   ma = nn.nhomnganh_ma,
                   ten = nn.nhomnganh_ten,
                   gioithieu = nn.nhomnganh_gioithieu,
                   active = nn.nhomnganh_active
               });
            string json = serializer.Serialize(list);
            context.Request.ContentType = "aplication/json";
            context.Response.Write(json);
        } 
        #endregion
        #region Render Nganh
        private void RenderSingleNganh(HttpContext context, int Id)
        {
            BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
            object obj = bussiness.Get<hn_nganh>(
               n => n.nganh_id.Equals(Id),
               n => new
               {
                   id = n.nganh_id,
                   nhomnganh_id = n.nhomnganh_id,
                   ma = n.nganh_ma,
                   ten = n.nganh_ten,
                   gioithieu = n.nganh_gioithieu,
                   active = n.nganh_active,

               });
            PropertyInfo info = obj.GetType().GetProperty("nhomnganh_id");
            var value = info.GetValue(obj, null);

            IList<object> list = bussiness.List<hn_nhomnganh>(
                null,
                null,
                n => n.nhomnganh_id.Equals(value),
                null,
                n => n.nhomnganh_id
                );
            List<Dictionary<string, object>> listnhomnganh = bussiness.RenderArrayMultiSelectJSON(HuongNghiepData.ViewNhomnganh(), new List<object> { value }, "id", "ten");
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("nganh", obj);
            dic.Add("nhomnganh", listnhomnganh);


            string json = serializer.Serialize(dic);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
        private void RenderNganh(HttpContext context)
        {
            BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
            IList<object> list = bussiness.List<hn_nganh>(
                null,
                null,
                null,
                n => n.nganh_ten,
                n => new
                {
                    id = n.nganh_id,
                    nhomma = n.nhomnganh_id,
                    ma = n.nganh_ma,
                    ten = n.nganh_ten,
                    gioithieu = n.nganh_gioithieu,
                    active = n.nganh_active,

                });
            string json = serializer.Serialize(list);
            context.Request.ContentType = "aplication/json";
            context.Response.Write(json);
        }
        public void RenderSelectNhomnganh(HttpContext context)
        {
            BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
            var nhomnganh = bussiness.RenderArrayMultiSelectJSON(HuongNghiepData.ViewNhomnganh(), null, "id", "ten");

            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("nhomnganh", nhomnganh);

            string json = serializer.Serialize(dic);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        } 
        #endregion
        #region Render Chuyen Nganh
        private void RenderSingleChuyennganh(HttpContext context, int Id)
        {
            BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
            object obj = bussiness.Get<hn_chuyennganh>(
               cn => cn.chuyennganh_id.Equals(Id),
               cn => new
               {
                   id = cn.chuyennganh_id,
                   nganhma = cn.hn_nganh.nganh_ten,
                   nganh_id = cn.hn_nganh.nganh_id,
                   ma = cn.chuyennganh_ma,
                   ten = cn.chuyennganh_ten,
                   gioithieu = cn.chuyennganh_gioithieu,
                   active = cn.chuyennganh_active
               });

            PropertyInfo info = obj.GetType().GetProperty("nganh_id");
            var value = info.GetValue(obj, null);

            IList<object> list = bussiness.List<hn_nganh>(
                null,
                null,
                n => n.nganh_id.Equals(value),
                null,
                n => n.nganh_id
                );
            List<Dictionary<string, object>> listnganh = bussiness.RenderArrayMultiSelectJSON(HuongNghiepData.ViewNganh(),new List<object>{value}, "id", "ten");
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("chuyennganh", obj);
            dic.Add("nganh", listnganh);


            string json = serializer.Serialize(dic);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
        private void RenderChuyennganh(HttpContext context)
        {
            BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
            IList<object> list = bussiness.List<hn_chuyennganh>(
                null,
                null,
                null,
                cn => cn.chuyennganh_ten,
                cn => new
                {
                    id = cn.chuyennganh_id,
                    nganhma = cn.hn_nganh.nganh_ten,
                    ma = cn.chuyennganh_ma,
                    ten = cn.chuyennganh_ten,
                    gioithieu = cn.chuyennganh_gioithieu,
                    active = cn.chuyennganh_active
                });
            string json = serializer.Serialize(list);
            context.Request.ContentType = "aplication/json";
            context.Response.Write(json);
        }
        public void RenderSelectNganh(HttpContext context)
        {
            BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
            var nganh = bussiness.RenderArrayMultiSelectJSON(HuongNghiepData.ViewNganh(), null, "id", "ten");

            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("nganh", nganh);

            string json = serializer.Serialize(dic);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        } 
        #endregion
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            return this;
        }
    }
}